package com.example.mylight;



import com.example.mylight.Timeservice.timereceiver;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;	
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.HandlerThread;
import android.view.KeyEvent;

public class MainActivity extends ActionBarActivity {
	Button clickbutton;         //锟斤拷锟斤拷锟斤拷锟斤拷
    Parameters parameters;
	Camera camera;
	Boolean isOn = false;       //锟斤拷锟斤拷锟斤拷欠锟斤拷卸锟�
	long exitTime =0;
	Boolean sos =false;          //sos锟竭筹拷锟角凤拷锟斤拷锟斤拷锟斤拷锟斤拷
	
	Button timebutton;         //sos锟斤拷锟斤拷
	Button remindbutton;       //锟斤拷锟斤拷时锟斤拷始锟斤拷钮
	EditText time;             
	int remindtime ;
	
	Button cancelbutton;       //取锟斤拷锟绞憋拷锟脚�
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		camera = Camera.open();
		parameters = camera.getParameters();
         
		startService(new Intent(this,Timeservice.class));
		bindupdatereceiver();
		Log.v("!", "start");
		time=(EditText)findViewById(R.id.editText1);             //锟襟定帮拷钮锟斤拷锟斤拷锟矫硷拷锟斤拷锟斤拷
		remindbutton=(Button)findViewById(R.id.button3);
		remindbutton.setOnClickListener(new remindlight());
		cancelbutton=(Button)findViewById(R.id.button4);
		cancelbutton.setOnClickListener(new canceltime());
		timebutton=(Button)findViewById(R.id.button2);
		timebutton.setOnClickListener(new timelight());
		clickbutton=(Button)findViewById(R.id.button1);          //锟斤拷锟斤拷锟斤拷钮锟斤拷锟斤拷
        clickbutton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(sos)                                          //锟叫断帮拷锟斤拷时锟斤拷锟絪os锟竭筹拷锟角凤拷锟斤拷锟斤拷锟叫ｏ拷锟斤拷锟斤拷锟斤拷应锟斤拷锟斤拷
				{
					//handler.removeCallbacks(updateThread);
					if(isOn)                                     //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷时锟斤拷sos锟竭筹拷锟斤拷锟斤拷锟叫ｏ拷锟斤拷锟斤拷锟斤拷锟阶刺拷锟斤拷锟揭拷锟斤拷泄氐坪锟斤拷锟斤拷锟斤拷锟斤拷锟�
					{
						offlight();
					}
					
					sos=false;
					timebutton.setTextColor(android.graphics.Color.WHITE);
				}
				if(isOn)
				{
					clickbutton.setBackgroundResource(R.drawable.off3);
					parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
					camera.setParameters(parameters);
					//camera.release();
					//camera = null;
					isOn = false;
				}
				else
				{
					clickbutton.setBackgroundResource(R.drawable.on3);
					//camera = Camera.open();
					//parameters = camera.getParameters();
					parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
					camera.setParameters(parameters);
					isOn = true;
				}
				
				//turnlight();                                 
			}
		});
	}
	
	//锟斤拷锟截灯匡拷锟斤拷转锟斤拷锟斤拷锟斤拷
			public void turnlight()
			{
				
				if (isOn) {
					//mFlashlightButton.setBackgroundResource(R.drawable.light_off);
					parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
					camera.setParameters(parameters);
					//camera.release();
					//camera = null;
					isOn = false;
					
				} else {
					//mFlashlightButton.setBackgroundResource(R.drawable.light_on);
					//camera = Camera.open();
					//parameters = camera.getParameters();
					parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
					camera.setParameters(parameters);
					isOn = true;
				}
			}
			
			//锟斤拷锟斤拷锟截灯猴拷锟斤拷
			public void offlight()
			{
				parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
				//camera.release();
				//camera = null;
				camera.setParameters(parameters);
				isOn = false;
				clickbutton.setBackgroundResource(R.drawable.off3);
			}
			
			//锟斤拷锟斤拷锟斤拷锟狡猴拷锟斤拷
			public void onlight()
			{
				parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
				camera.release();
				camera = null;
				isOn = true;
			}
			
			//锟斤拷锟矫伙拷锟斤拷锟斤拷锟斤拷陌锟斤拷锟斤拷锟�
			@Override
			public boolean onKeyDown(int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				
				switch (keyCode) {
				case KeyEvent.KEYCODE_BACK:
					exit();                       //锟斤拷锟斤拷锟斤拷锟斤拷
					return false;

				case KeyEvent.KEYCODE_VOLUME_DOWN:
					if(isOn)
					{
					   offlight();
					   
					}
					sos=false;
					break;
					
				case KeyEvent.KEYCODE_VOLUME_UP:
					if(isOn)
					{
					   offlight();
					   
					}
					sos=false;
					 break;
			     }
				
				return super.onKeyDown(keyCode, event);

			}
			
			//锟斤拷锟截硷拷锟斤拷蟠ィ锟斤拷诠娑ㄊ憋拷锟斤拷诘锟斤拷锟斤拷锟轿凤拷锟截硷拷锟斤拷瞥锟�
			private void exit() {
				// TODO Auto-generated method stub
				if (System.currentTimeMillis() - exitTime > 2000) {
					Toast.makeText(getApplicationContext(), R.string.exit,
							Toast.LENGTH_SHORT).show();
					exitTime = System.currentTimeMillis();
				} else {
					if (camera != null) {
						camera.release();
						camera = null;

					}
					finish();
					System.exit(0);
				}
			}
			
			//实锟斤拷SOS锟斤拷锟斤拷
			Handler handler =new Handler();
			class timelight implements OnClickListener
			{
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Log.v("!", "star");
					//timebutton.setVisibility(View.INVISIBLE);
					//turnlight();
					clickbutton.setBackgroundResource(R.drawable.off3);   //锟饺把筹拷锟斤拷锟斤拷钮锟截憋拷
					
					if(sos)
					{
						
						sos=false;
						if(isOn)  //锟叫断帮拷锟铰关憋拷SOS时锟斤拷锟狡癸拷锟角凤拷为锟斤拷状态锟斤拷锟斤拷为锟斤拷锟斤拷要锟斤拷锟斤拷锟截憋拷 锟斤拷锟斤拷夭锟斤拷锟�
						{
						   offlight();
						}
					}
					else
					{
						timebutton.setTextColor(android.graphics.Color.YELLOW);
						sos=true;
					}
					handler.post(updateThread);
					
				}
				
			}
			
			//SOS锟斤拷run锟斤拷锟斤拷实锟街ｏ拷锟斤拷要锟斤拷postdelayed实锟斤拷锟斤拷时锟斤拷
			Runnable updateThread =new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
				    Log.v("!", "count");
				    if(sos)
				    {
					    turnlight();
					    handler.postDelayed(updateThread, 350);		    
				    }
				    else
				    {
				    	
				    	handler.removeCallbacks(updateThread);
				    	timebutton.setTextColor(android.graphics.Color.WHITE);
				    	
				    }
				}
			};
			
			//实锟街碉拷锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷
			Handler timehandler =new Handler();
			class remindlight implements OnClickListener
			{

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					remindbutton.setBackgroundResource(R.drawable.started);
					if(time.getText().toString().equals("")||time.getText().toString().equals("0"))
					{   //锟叫讹拷锟角凤拷为锟秸硷拷锟斤拷
						Toast.makeText(getApplicationContext(), R.string.itsnull,
								Toast.LENGTH_SHORT).show();
					}
					else
					{
						remindtime=Integer.parseInt(time.getText().toString());
						remindbutton.setVisibility(View.INVISIBLE);
						cancelbutton.setVisibility(View.VISIBLE);
						Log.v("!", "start");
						Intent intent =new Intent(Timeservice.broadcast_timelight_control); 
						intent.putExtra("command", 1);   //锟斤拷装锟斤拷锟斤拷
						intent.putExtra("remindtime", remindtime);//锟斤拷装时锟斤拷锟斤拷锟�
						intent.putExtra("sos", sos);       //锟斤拷装锟斤拷锟斤拷锟阶刺�
						intent.putExtra("isOn", isOn);
						sendBroadcast(intent);
						Log.v("!", "sended");
					}
				}
				
			}
			
			class updatereceiver extends BroadcastReceiver
			{

				@Override
				public void onReceive(Context context, Intent intent) {
					// TODO Auto-generated method stub
					int i=intent.getIntExtra("buttonstatus", 0);
					boolean over =intent.getBooleanExtra("over", false);
					remindtime=intent.getIntExtra("remindtime", 0);
					if(remindtime==0)
					{
						//锟斤拷时锟斤拷锟斤拷 锟斤拷edittext锟斤拷眨锟斤拷锟绞硷拷锟斤拷锟斤拷锟脚ワ拷锟斤拷锟�
						camera.release();
						time.setText("");
						remindbutton.setVisibility(View.VISIBLE);
        				cancelbutton.setVisibility(View.INVISIBLE);
        				
					}
					else
					{
						//锟斤拷editext锟斤拷锟斤拷时锟斤拷
						time.setText(Integer.toString(remindtime));
					    Log.v("!", "settext");
					}
					if(over)
					{
						rebulidcamera();
					}
					
					
				}
				
			}
			
			public void rebulidcamera()
			{
				camera = Camera.open();
				parameters = camera.getParameters();
			}
			updatereceiver receiver;
			private void bindupdatereceiver()
			{
				receiver= new updatereceiver();
				IntentFilter filter =new IntentFilter(Timeservice.broadcast_update);
				registerReceiver(receiver, filter);
			}
			//锟斤拷锟斤拷时run锟斤拷锟斤拷
			
		/*	Runnable remindThread =new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					if(remindtime==0)
					{
						timehandler.removeCallbacks(remindThread);
						System.out.println("over");
						time.setText("");
					}
					else
					{
					   time.setText(Integer.toString(remindtime--));
					   
					  timehandler.postDelayed(remindThread,1000);
					}
				}
			};*/
			//取锟斤拷锟绞憋拷锟脚�
			class canceltime implements OnClickListener
			{

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					remindbutton.setVisibility(View.VISIBLE);
					cancelbutton.setVisibility(View.INVISIBLE);
					cancelbutton.setBackgroundResource(R.drawable.canceled);
					//timehandler.removeCallbacks(remindThread);
					remindtime=0;
					time.setText("");
					Intent intent1=new Intent(Timeservice.broadcast_timelight_control);
					intent1.putExtra("command", 3);
					sendBroadcast(intent1);
				}
				
			}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		camera.release();
		super.onDestroy();
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		camera.release();
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		//camera = Camera.open();
		//parameters = camera.getParameters();
		super.onResume();
	}
	
	

}
